package com.lc.afbx.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lc.afbx.R;
import com.lc.afbx.adapters.ProductAdapter;
import com.lc.afbx.conn.GetApiInsuranceAccident;
import com.lc.afbx.conn.GetApiShowICompany;
import com.lc.afbx.util.PrefrenceConfig;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilToast;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/5/19.
 */
public class AccidentInsuranceActivity extends BaseActivity implements View.OnClickListener {

    private ListView accident_insurance_lv;
    private RelativeLayout accident_insurance_companyName_rl;
    private TextView accident_insurance_companyName_tv;

    private List<GetApiInsuranceAccident.Data> productList = new ArrayList<>();
    private List<GetApiShowICompany.Data> dataList = new ArrayList<>();
    private List<String> companyList = new ArrayList<>();
    private ProductAdapter adapter;

    //请求下拉保险公司的接口
    private GetApiShowICompany getApiShowICompany = new GetApiShowICompany(new AsyCallBack<GetApiShowICompany.Info>() {
        @Override
        public void onSuccess(String toast, int type, GetApiShowICompany.Info info) throws Exception {

            dataList = info.dataList;
            for (int i = 0; i < dataList.size(); i++) {
                companyList.add(dataList.get(i).title);
            }

            accident_insurance_companyName_tv.setHint(R.string.insurance_company_hint);

        }

        @Override
        public void onFail(String toast, int type) throws Exception {

            UtilToast.show(context, toast);
        }
    });

    //产品列表的接口
    private GetApiInsuranceAccident getApiInsuranceAccident = new GetApiInsuranceAccident(new AsyCallBack<GetApiInsuranceAccident.Info>() {
        @Override
        public void onSuccess(String toast, int type, GetApiInsuranceAccident.Info info) throws Exception {

            productList.addAll(info.dataList);

            adapter.notifyDataSetChanged();

        }

        @Override
        public void onFail(String toast, int type) throws Exception {

            UtilToast.show(context, toast);
            adapter.notifyDataSetChanged();

        }
    });


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_accident_insurance);

        accident_insurance_companyName_rl = (RelativeLayout) findViewById(R.id.accident_insurance_companyName_rl);

        accident_insurance_companyName_tv = ((TextView) findViewById(R.id.accident_insurance_companyName_tv));

        accident_insurance_lv = ((ListView) findViewById(R.id.accident_insurance_lv));

        accident_insurance_lv.setAdapter(adapter = new ProductAdapter(context, productList));

        accident_insurance_lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Intent intent = new Intent(context, AccidentInsuranceDetailActivity.class);

                intent.putExtra(PrefrenceConfig.INTENT_TAG, productList.get(position).id);

                Bundle bundle = new Bundle();
                //这个是保险公司的id  如果不对 去popwindow 的datalist 里找
                bundle.putString("insurance_id", getApiInsuranceAccident.cid);

                intent.putExtras(bundle);

                context.startActivity(intent);
            }
        });

        getApiShowICompany.tid = "3";
        getApiShowICompany.execute(this);


        accident_insurance_companyName_rl.setOnClickListener(this);


    }


    @Override
    public void onClick(View view) {

        switch (view.getId()) {

            case R.id.back:
                finish();
                return;

            case R.id.accident_insurance_companyName_rl:
                showPopupWindow(view);
                return;


        }


    }

    private void showPopupWindow(View view) {

        View contentView = getLayoutInflater().inflate(R.layout.pop_layout, null);

        final ListView popList = (ListView) contentView.findViewById(R.id.item_popList);

        ArrayAdapter adapter = new ArrayAdapter(context, R.layout.support_simple_spinner_dropdown_item, companyList);

        popList.setAdapter(adapter);

        final PopupWindow popupWindow = new PopupWindow(contentView, accident_insurance_companyName_rl.getWidth(), ViewGroup.LayoutParams.WRAP_CONTENT, true);

        popupWindow.setTouchable(true);

        popupWindow.setTouchInterceptor(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return false;
            }
        });

        popupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.shape_white_bg));

        popupWindow.showAsDropDown(view);

        popList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                accident_insurance_companyName_tv.setText(dataList.get(position).title);

                productList.clear();

                getApiInsuranceAccident.cid = dataList.get(position).id;
                getApiInsuranceAccident.execute(context);

                popupWindow.dismiss();
            }
        });

    }


}
